
import 'package:flutter/material.dart';
import 'package:flutter_deer/account/iview/agent_user_betting_list_iview.dart';
import 'package:flutter_deer/account/presenter/agent_user_betting_list_presenter.dart';
import 'package:flutter_deer/account/provider/agent_user_betting_person_list_provider.dart';
import 'package:flutter_deer/index/index_router.dart';
import 'package:flutter_deer/index/models/user_betting_entity.dart';
import 'package:flutter_deer/mvp/base_page.dart';
import 'package:flutter_deer/mvp/power_presenter.dart';
import 'package:flutter_deer/routers/fluro_navigator.dart';
import 'package:flutter_deer/widgets/my_refresh_list.dart';
import 'package:flutter_deer/widgets/state_layout.dart';
import 'package:flutter_deer/util/other_utils.dart';
import 'package:flutter_deer/util/theme_utils.dart';
import 'package:flutter_deer/widgets/my_card.dart';
import 'package:flutter_deer/res/resources.dart';
import 'package:flutter_gen/gen_l10n/deer_localizations.dart';
import 'package:intl/intl.dart';
import 'package:provider/provider.dart';


class AgentUserBettingPersonListPage extends StatefulWidget {

  const AgentUserBettingPersonListPage({
    super.key,
    required this.status,
    required this.username
  });

  final String? status;
  final String? username;

  @override
  _AgentUserBettingPersonListPageState createState() => _AgentUserBettingPersonListPageState();
}

class _AgentUserBettingPersonListPageState extends State<AgentUserBettingPersonListPage> with AutomaticKeepAliveClientMixin<AgentUserBettingPersonListPage>,BasePageMixin<AgentUserBettingPersonListPage, PowerPresenter>, SingleTickerProviderStateMixin implements AgentUserBettingListIMvpView {

  late AgentUserBettingListPresenter agentUserBettingListPresenter;
  AgentUserBettingPersonListPageProvider provider = AgentUserBettingPersonListPageProvider();
  @override
  PowerPresenter<dynamic> createPresenter() {
    final PowerPresenter<dynamic> powerPresenter = PowerPresenter<dynamic>(this);
    agentUserBettingListPresenter = new AgentUserBettingListPresenter();
    powerPresenter.requestPresenter([agentUserBettingListPresenter]);
    return powerPresenter;
  }

  @override
  void initState() {
    super.initState();
    if(mounted){
      _onRefresh();
    }
  }

  @override
  void dispose() {
    super.dispose();
  }

  Future<dynamic> _onRefresh() async {
    Future.delayed(Duration(seconds: 1),()=>{agentUserBettingListPresenter.getUserBettingList(widget.status.nullSafe, widget.username.nullSafe)});
  }

  Future<dynamic> _loadMore() async {
    agentUserBettingListPresenter.getMoreUserBettingList(widget.status.nullSafe,widget.username.nullSafe, provider.currentPage!+1);
  }

  @override
  void setUserBettingPage(UserBettingPageEntity? pageEntity){
    provider.setBettingPage(pageEntity);
  }

  @override
  Widget build(BuildContext context) {
    super.build(context);
    final TextStyle? textTextStyle = Theme.of(context).textTheme.bodyMedium?.copyWith(fontSize: Dimens.font_sp12);
    final bool isDark = context.isDark;
    return ChangeNotifierProvider<AgentUserBettingPersonListPageProvider>(
        create: (_) => provider,
        child: Consumer<AgentUserBettingPersonListPageProvider>(
            builder: (_,provider,child){
              if(provider.stateType==StateType.loading){
                return StateLayout(type: StateType.loading);
              }
              return DeerListView(
                  itemCount: provider.list!.length!,
                  stateType: provider.stateType!,
                  onRefresh: _onRefresh,
                  loadMore: _loadMore,
                  hasMore: provider.hasMore!,
                  itemBuilder: (_, index) {
                    final UserBettingEntity? item = provider.list!.elementAt(index);
                    Text remarkText = Text('');
                    if(widget.status=='1'){
                      remarkText = Text('+'+item!.winScore.toString(),style: TextStyle(color: Colors.red),);
                    }else if(widget.status=='2'){
                      remarkText = Text('-'+item!.score.toString(),style: TextStyle(color:Colors.green),);
                    }
                    return GestureDetector(
                      onTap: (){
                        NavigatorUtils.push(context, IndexRouter.bettingDetailPage,arguments: item.id.toString());
                      },
                      child: Padding(
                          padding: const EdgeInsets.symmetric(horizontal: 10.0,vertical: 5),
                          child: MyCard(
                              child: Padding(
                                padding: const EdgeInsets.symmetric(horizontal: 5),
                                child: Column(
                                    crossAxisAlignment: CrossAxisAlignment.start,
                                    children: <Widget>[
                                      SizedBox(height: 5,),
                                      Row(
                                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                        children: [
                                          Text('${DeerLocalizations.of(context)!.playWays.nullSafe}'+(item!.playWay=='1'?"${DeerLocalizations.of(context)!.gamePositiveWave.nullSafe}":'${DeerLocalizations.of(context)!.gameAntiWave.nullSafe}')),
                                          Text('${DeerLocalizations.of(context)!.orderTime.nullSafe}'+item!.createOrderTime.nullSafe)
                                        ],
                                      ),
                                      SizedBox(height: 10,),
                                      Row(
                                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                        children: [
                                          Text('${DeerLocalizations.of(context)!.name.nullSafe}:'+item!.name.nullSafe),
                                          remarkText
                                        ],
                                      ),
                                      SizedBox(height: 10,),
                                      Row(
                                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                        children: [
                                          Text('${DeerLocalizations.of(context)!.type.nullSafe}:'+(item!.bettingType=='1'?'${DeerLocalizations.of(context)!.score.nullSafe}':'${DeerLocalizations.of(context)!.winOrLose.nullSafe}')),
                                          Text('${DeerLocalizations.of(context)!.bettingAmount.nullSafe}:'+NumberFormat.decimalPattern('en_us').format(item!.score).toString())
                                        ],
                                      ),
                                      SizedBox(height: 8,),
                                    ]
                                ),
                              )
                          )
                      )
                    );
                  }
              );
            }
        )
    );
  }

  @override
  bool get wantKeepAlive => false;
}
